﻿@model Sys_Admin
@{
    ViewBag.ParentUrl = "/Order/Edit";
    ViewBag.Title = "新单购买";
}
@section header{
    <script language="javascript" src="/js/LodopFuncs.js"></script>
}
@section breadcrumb
{
    <ul class="breadcrumb">
        <li>
            <i class="ace-icon fa fa-home home-icon"></i>
            <a href="/">首页</a>
        </li>
        <li class="active">@ViewBag.Title</li>
    </ul><!-- .breadcrumb -->
}

<div class="row">
    <div class="col-xs-12">
        <div class="widget-box transparent">
            <div class="widget-header widget-header-flat">
                <h4 class="smaller">
                    <i class="icon-search"></i>
                    基本信息
                </h4>
            </div>

            <div class="widget-body">
                <div class="widget-main">
                    <div class="row">
                        <div class="col-xs-12 col-sm-6">
                            <div class="form-inline">
                                <label>编码导入：</label>
                                <input type="text" class="input-large" placeholder="编码">

                                <button type="button" class="btn btn-info btn-sm">
                                    <i class="ace-icon fa fa-key bigger-110"></i>导入
                                </button> |
                                <button type="button" class="btn btn-info btn-sm">
                                    <i class="ace-icon fa fa-key bigger-110"></i>PNR内容导入
                                </button>
                            </div>
                            <span class="help-block">多人导入票号格式：999-9999999970-75.</span>

                            <code>公告：本系统已全面支持PNR内容导入方式，支持国内、国际航段导入</code>
                        </div>
                        <div class="col-xs-12 col-sm-2">
                            <div class="well">
                                <h4 class="green smaller lighter">身份证阅读器状态：</h4>
                                读卡器失败
                            </div>
                        </div>
                        <div class="col-xs-12 col-sm-2">
                            <div class="well">
                                <h4 class="green smaller lighter">发票剩余次数：</h4>
                                @ViewBag.InvoiceCount 次
                            </div>
                        </div>
                        <div class="col-xs-12 col-sm-2">
                            <div class="well">
                                <h4 class="green smaller lighter">提取次数：</h4>
                                @ViewBag.InterfaceCount 次
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <form id="frm" action="" method="post">
            <div class="widget-box">
                <div class="widget-header widget-header-flat">
                    <h4 class="smaller">
                        <i class="icon-search"></i>
                        产品信息
                    </h4>
                </div>

                <div class="widget-body">
                    <div class="widget-main">
                        <div class="profile-user-info profile-user-info-striped">
                            <div class="profile-info-row">
                                <div class="profile-info-name">
                                    <code>*</code> 产品计划：
                                </div>

                                <div class="profile-info-value">

                                    @Html.DropDownList("Product", (IEnumerable<SelectListItem>)TempData["ProductList"], new { @class = "col-xs-12 col-sm-3", style = "width: 360px;" })
                                    <span class="help-block" id="remark" style="margin-top: 32px;">
                                    </span>
                                </div>
                            </div>

                            <div class="profile-info-row">
                                <div class="profile-info-name"> 起保日期： </div>

                                <div class="profile-info-value">
                                    <input type="text" class="input-large" placeholder="点击选择" id="stime" name="stime" required="required" readonly />
                                    <i class="fa fa-calendar light-orange bigger-110"></i>
                                </div>
                            </div>

                            <div class="profile-info-row" id="divticket">
                                <div class="profile-info-name"> 单证印刷号： </div>

                                <div class="profile-info-value" id="ticketcount">
                                    @*                                @if (ViewBag.TicketCount < 1)*@
                                    @*                                {*@
                                    @*                                    <span class="editable">已无可用的单证，请先领用单证 <code>* 单证号务必和纸质单证号码保持一致</code> </span>*@
                                    @*                                }*@
                                    @*                                else*@
                                    @*                                {*@
                                    @*                                    <input type="text" class="input-large" placeholder="单证印刷号" id="ticket" name="ticket" />*@
                                    @*                                }*@
                                </div>
                            </div>

                            <div class="profile-info-row">
                                <div class="profile-info-name"> 航班号： </div>

                                <div class="profile-info-value">
                                    <input type="text" class="input-large" placeholder="航班号" id="flight" name="flight" />
                                </div>
                            </div>

                        </div>

                    </div>
                </div>

            </div>


            <div class="col-xs-12 col-sm-12 widget-container-col">
                <div class="widget-box widget-color-dark transparent">
                    <div class="widget-header">
                        <h2 class="widget-title"><i class="ace-icon fa fa-leaf green"></i>被保人信息</h2>
                        <div class="widget-toolbar">
                            <a href="#" data-action="collapse">
                                <i class="ace-icon fa fa-chevron-up bigger-125"></i>
                            </a>
                        </div>

                        <div class="widget-toolbar no-border">
                            <a href="/static/template.xls">【团单模板下载.xls】</a>
                            <a class="btn btn-xs btn-yellow bigger" href="###" id="btnUpexcel">
                                <i class="ace-icon fa fa-cloud-upload"></i>
                                上传团单
                            </a>
                            <a class="btn btn-xs btn-info bigger" id="btnTd" href="###">
                                通用团单
                            </a>
                            <a class="btn btn-xs btn-info bigger" id="btnAddTd" href="###">
                                增加被保人
                            </a>
                            <a class="btn btn-xs btn-info bigger" id="btnDelItem" href="###">
                                删除选中项
                            </a>
                        </div>
                    </div>

                    <div class="widget-body">
                        <div class="widget-main">
                            <div>
                                <table class="table table-striped table-bordered" id="tabList">
                                    <thead>
                                        <tr>
                                            <th class="center">#No.</th>
                                            <th>姓名</th>
                                            <th>证件类型</th>
                                            <th>证件号码</th>
                                            <th>出生日期</th>
                                            <th>手机号</th>
                                            <th>性别</th>
                                            <th>分数</th>
                                            <th>受益人</th>
                                            <th>
                                                <label class="position-relative">
                                                    <input type="checkbox" class="ace" id="chkAll" />
                                                    <span class="lbl"></span>
                                                </label>
                                            </th>
                                        </tr>
                                    </thead>

                                    <tbody>
                                        <tr>
                                            <td class="center">1</td>

                                            <td>
                                                <input type="text" class="input-large" placeholder="姓名" name="txtName" required="required">
                                            </td>
                                            <td>
                                                <select class="form-control" name="selType" style="width:100px;">
                                                    <option value="0">身份证</option>
                                                    <option value="1">护照</option>
                                                    <option value="2">军官证</option>
                                                    <option value="3">其他</option>
                                                </select>
                                            </td>
                                            <td>
                                                <input type="text" class="input-large" placeholder="证件号码" name="txtZj" required="required">
                                            </td>
                                            <td>
                                                <input type="text" class="input-large" placeholder="如:1990-09-17" value="1980-01-01" name="txtDate" required="required" style="line-height: normal;">
                                            </td>
                                            <td>
                                                <input type="tel" class="input-large" placeholder="手机号" name="txtTel">
                                            </td>

                                            <td>
                                                <select class="form-control" name="selSex" style="width:80px; ">
                                                    <option value="0">女士</option>
                                                    <option value="1">先生</option>
                                                </select>
                                            </td>
                                            <td>
                                                <input type="number" class="input-mini" placeholder="份数" min="1" max="10" name="txtCount" required="required" value="1">
                                            </td>
                                            <td>
                                                <input type="text" class="input-small" placeholder="受益人" name="txtSyr">
                                            </td>
                                            <td>
                                                <label class="position-relative">
                                                    <input type="checkbox" class="ace" name="chkItem" />
                                                    <span class="lbl"></span>
                                                </label>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>

                            <div class="hr hr8 hr-double hr-dotted"></div>

                            <div class="row">
                                <div class="col-sm-5 pull-right">
                                    <button class="btn btn-xs btn-inverse bigger pull-right accordion-toggle" data-toggle="collapse" href="#toubao">
                                        显示/隐藏投保人信息
                                    </button>
                                </div>
                                <div class="col-sm-7 pull-left"> ... </div>
                            </div>

                            <div class="row collapse" id="toubao">

                                <h3 class="header smaller lighter blue">
                                    投保人信息
                                    <small>根据保监会规定，未成年人（未满18周岁）必须填写成年人作为投保人</small>
                                </h3>

                                <div>
                                    <table class="table table-bordered">
                                        <tbody>
                                            <tr>
                                                <td style="text-align: right;background-color:#f3f3f3;width: 150px;vertical-align: middle;">
                                                    <code>*</code>投保人：
                                                </td>

                                                <td>
                                                    <input type="text" class="input-large" placeholder="投保人" id="txtToubaoren">
                                                </td>
                                                <td style="text-align: right;background-color:#f3f3f3;width: 150px;vertical-align: middle;">
                                                    <code>*</code>性别：
                                                </td>
                                                <td>
                                                    <div class="radio" style="float:left;">
                                                        <label>
                                                            <input name="rdoSex" type="radio" class="ace" value="0">
                                                            <span class="lbl"> 女士</span>
                                                        </label>
                                                    </div>
                                                    <div class="radio" style="float:left;margin-top:10px;">
                                                        <label>
                                                            <input name="rdoSex" type="radio" class="ace" value="1">
                                                            <span class="lbl"> 先生</span>
                                                        </label>
                                                    </div>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td style="text-align: right;background-color:#f3f3f3;width: 150px;vertical-align: middle;">
                                                    <code>*</code>证件号：
                                                </td>

                                                <td>
                                                    <select class="form-control" id="selType" style="width:160px; float: left;">
                                                        <option value="0">身份证</option>
                                                        <option value="1">护照</option>
                                                        <option value="2">军官证</option>
                                                        <option value="3">其他</option>
                                                    </select>&nbsp;&nbsp;
                                                    <input type="text" class="input-large" placeholder="证件号" id="txtZhengjianId">
                                                </td>
                                                <td style="text-align: right;background-color:#f3f3f3;width: 150px;vertical-align: middle;">
                                                    出生日期：
                                                </td>
                                                <td>
                                                    <input type="text" class="input-large" placeholder="如:1990-09-17" value="1980-01-01" id="birthday" style="line-height: normal;">
                                                    <i class="fa fa-calendar light-orange bigger-110"></i>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="space-6"></div>
                            <div class="clearfix form-actions">
                                <div class="col-md-offset-3 col-md-9">
                                    @Html.TextBox("isprint")
                                    <button class="btn btn-info" type="button" id="btnSubmit">
                                        <i class="ace-icon fa fa-check bigger-110"></i>
                                        投保
                                    </button>

                                    &nbsp; &nbsp; &nbsp;
                                    <button class="btn btn-danger" type="button" id="btnSubmitPrint">
                                        <i class="ace-icon fa fa-print bigger-110"></i>
                                        投保并打印
                                    </button>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
@section footerScript
{
    <script src="/assets/js/validation/jquery-html5Validate.js"></script>
    <script src="@Url.Content("~/assets/js/file-upload/ajaxupload.js")" type="text/javascript"></script>
    <script src="/assets/js/laydate/laydate.js"></script>
    <script src="/assets/js/layer/layer.js"></script>
}
@section footer{
    <script>

        var LODOP; //声明为全局变量

        /**
        * 投保验证
        */
        $("#frm").html5Validate(function () {
            //this.submit();
            $('#btnSubmit').prop('disabled', true);

            var isprint = $('#isprint').val();
            var obj = {}; //提交的数据

            obj.Insured = []; //被保人可能多个


            var val = $('#Product').children('option:selected').val();
            if (val) {
                obj.ProductId = val.split('###')[0]; //产品ID
            }

            obj.AppntName = $('#txtToubaoren').val();
            obj.AppntSex = $(":radio[name='rdoSex']:checked").val();
            obj.AppntIdType = $('#selType').val();
            obj.AppntIdNo = $('#txtZhengjianId').val();
            obj.AppntBirthday = $('#birthday').val();

            obj.CvaliDate = $('#stime').val();
            obj.Flight = $('#flight').val();


            //被保人信息
            $('#tabList').find('tbody tr').each(function () {
                var item = $(this);
                obj.Insured.push({
                    Name: item.find("input:eq(0)").val(),
                    IdType: item.find("select:eq(0)").val(),
                    IdNo: item.find("input:eq(1)").val(),
                    Birthday: item.find("input:eq(2)").val(),
                    Phone: item.find("input:eq(3)").val(),
                    Sex: item.find("select:eq(1)").val(),
                    Mult: item.find("input:eq(4)").val(),
                    BnfName: item.find("input:eq(5)").val()
                });
            });

            $.post("/order/policy", { obj: obj }, function (o) {
                console.log(o);
                var rs = eval(o);
                if (rs.Code != 0) {
                    $('#btnSubmit').prop('disabled', false);
                } else { //投保成功
                    if (isprint) {
                        var arr = rs.Data;//JSON.parse();
                        console.log(arr);

                        LODOP = getLodop();
                        LODOP.PRINT_INIT("保单打印");

                        var tpl = rs.Data.tpl;
                        //添加打印
                        rs.Data.list.forEach(function (o) {
                            if (o) {
                                var oid = o.O_Id;
                                eval(tpl);
//                                LODOP.NewPage();
//                                LODOP.ADD_PRINT_RECT(10, 18, 128, 160, 0, 1);
//                                LODOP.ADD_PRINT_TEXT(31, 33, 111, 20, "保单编号：" + o.O_Id);
                            }
                        });
                        LODOP.PREVIEW();

                    }
                }

                alert(rs.Msg);
            });

        }, {

        });


        laydate.render({
            elem: '#stime',
            type: 'datetime'
        });
        $(function () {

            $('#Product').change(function () {
                var val = $(this).children('option:selected').val();
                if (val) {
                    var val2 = val.split('###');
                    $("#remark").html(val2[1]);

                    $.post("/order/TicketCount", { id: val2[0] }, function (o) {

                        var html = '<span class="editable">已无可用的单证，请先领用单证 <code>* 单证号务必和纸质单证号码保持一致</code> </span>';
                        if (o != 'err') {
                            //console.log(o);
                            var rs = eval(o);
                            if (rs.remain > 0) {
                                html = '<span class="editable">当前单证号：' + rs.curno + ',剩余次数：' + rs.remain + '次</span>';
                            }
                        } else {
                            //alert("单证获取错误");
                        }
                        $('#ticketcount').html(html);
                    });
                }
            });
            $('#Product').trigger('change');

            /**
            * 投保
            **/
            $('#btnSubmit').click(function () {
                $('#frm').submit();
            });

            /**
            * 投保并打印
            **/
            $('#btnSubmitPrint').click(function () {
                $('#isprint').val(1);
                $('#frm').submit();
            });
            /**
            *通用团单
            */
            $('#btnTd').click(function () {
                layer.open({
                    type: 1 //Page层类型
                    ,
                    area: ['700px', '380px'],
                    title: '团单导入',
                    shade: 0.6 //遮罩透明度
                    ,
                    maxmin: false //允许全屏最小化
                    ,
                    anim: 1 //0-6的动画形式，-1不开启
                    ,
                    content: '<div class="widget-main"><label>方式二：复制客人信息到下面的文本框中 然后点击导入 <code>*格式：姓名,证件号码</code></label>' +
                        '<textarea class="form-control" rows="10" style="resize:vertical" id="txtImportTd" placeholder="张三,420103195405250821"></textarea>' +
                        '</div>',
                    btn: ['确定', '取消'],
                    yes: function (index) {
                        var arr = $('#txtImportTd').val().split('\n');
                        var tr = $('#tabList tr:last').html(); //最后一个tr
                        arr.forEach(function (o) {
                            if (o) {
                                var v = o.split(','); //分割每一个用户数据，
                                var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                                if (reg.test(v[1]) === false) {
                                    alert("身份证输入不合法");
                                    return false;
                                }

                                var birthday = v[1].substring(6, 10) + "-" + v[1].substring(10, 12) + "-" + v[1].substring(12, 14);
                                var sex = parseInt(v[1].substr(16, 1)) % 2 == 1 ? 1 : 0;

                                $('#tabList').append('<tr>' + tr + '</tr>');
                                //debugger;
                                $('#tabList tr:last').find("td").eq(1).find('input:first').val(v[0]);
                                $('#tabList tr:last').find("td").eq(3).find('input:first').val(v[1]);
                                $('#tabList tr:last').find("td").eq(4).find('input:first').val(birthday);
                                $('#tabList tr:last').find("td").eq(6).find('select:first').val(sex);
                            }
                        });

                        $('#tabList tr').each(function (i, e) {
                            $(e).children('td:first').html(i);
                        });
                        //layer.close(index);
                        layer.msg('批量追加到被保人列表', {
                            icon: 6,
                            btn: ['确定']
                        }, function () {
                            layer.close(index);
                        });
                    }
                });

            });
            /**
            * 增加被保人
            */
            $('#btnAddTd').click(function () {
                var tr = $('#tabList tr:last').html(); //最后一个tr
                $('#tabList').append('<tr>' + tr + '</tr>');
                //var itemid = parseInt($('#tabList tr:last td:first').html()) + 1;

                $('#tabList tr').each(function (i, e) {
                    $(e).children('td:first').html(i);
                });
            });

            /**
            * 全选/反选
            **/
            $('#chkAll').click(function () {
                var state = $(this).prop('checked');
                $('input[name="chkItem"]').prop('checked', state);
            });

            /**
            * 批量删除选中项
            **/

            $('#btnDelItem').click(function () {
                var trsnot = $('tr').find('input[name="chkItem"]:not(:checked)');
                if (trsnot.length < 1) {
                    layer.alert('必须至少保留一条数据', { icon: 5 });
                    return;
                }


                var trs = $('tr').find('input[name="chkItem"]:checked').parents('tr');

                if (trs.length > 0) {
                    layer.confirm("您确定要删除选中项", { btn: ['确定', '取消'] },
                        function (i) {
                            layer.close(i);
                            trs.remove();
                            $('#tabList tr').each(function (i, e) {
                                $(e).children('td:first').html(i);
                            });
                        }
                    );
                } else {
                    layer.msg('没有选中项！', { icon: 5 });
                }


            });
        });


        layer.ready(function () {

        });

        var layindex;

        //上传通用团单
        new AjaxUpload('#btnUpexcel', {
            action: '/Passport/UpExcel',
            data: { Folder: "policy", Type: 'Excel' },
            onSubmit: function (file, ext) {
                //                if (!/^(jpeg|jpg|gif|png|bmp)$/.test(ext)) {
                //                    alert("只能上传格式为(jpeg|jpg|gif|png|bmp)格式的文件！", "提示");
                //                    return false;
                //                } else {
                //                    $("#updataReco").find("div").remove();
                //                    $("#updataReco").html("<img id='loadimgs' src='/lib/file-upload/loading.gif'/>");
                //                }

                if (!/^(xlsx|xls)$/.test(ext)) {
                    alert("只能上传格式为(xlsx|xls)格式的文件！", "提示");
                    return false;
                } else {
                    layindex = layer.load();
                }
            },
            onComplete: function (file, response) {
                layer.close(layindex);
                var responseText = response.toString();
                if (/MSIE 6.0/i.test(navigator.userAgent)) {
                }

                var o = JSON.parse(responseText);
                if (o.Code != 0) {
                    layer.msg('上传失败。。', { icon: 5 });
                    return;
                }
                var arr = JSON.parse(o.Data); //excel中的数据

                var tr = $('#tabList tr:last').html(); //最后一个tr
                arr.forEach(function (o) {
                    if (o) {
                        $('#tabList').append('<tr>' + tr + '</tr>');
                        var dd = new Date(o.出生日期);
                        var sex = o.性别 == "男" ? 1 : 0;

                        //debugger;
                        $('#tabList tr:last').find("td").eq(1).find('input:first').val(o.被保人姓名);
                        $('#tabList tr:last').find("td").eq(2).find("select:first option:contains('" + o.证件类型 + "')").prop("selected", true);
                        $('#tabList tr:last').find("td").eq(3).find('input:first').val(o.证件号码);
                        $('#tabList tr:last').find("td").eq(4).find('input:first').val(dd.getFullYear() + '-' + (dd.getMonth() + 1) + '-' + dd.getDate());
                        $('#tabList tr:last').find("td").eq(5).find('input:first').val(o.手机号);
                        $('#tabList tr:last').find("td").eq(6).find('select:first').val(sex);
                        $('#tabList tr:last').find("td").eq(7).find('input:first').val(o.购买份数);
                    }
                });

                $('#tabList tr').each(function (i, e) {
                    $(e).children('td:first').html(i);
                });
                //
                //
                //                //显示1
                //                var reArray = responseText.split('|');
                //                if (reArray.length < 2) { //异常
                //                    alert(reArray[0]);
                //                    return false;
                //                } else if (reArray[0] == null || reArray[0] == "") {
                //                    alert("上传失败！");
                //                    return false;
                //                } else {
                //                    //用于记录，上传的图片
                //                    var newV = "/" + reArray[1] + "/" + reArray[0];
                //                    //                    $("#p_ReComImg").val(newV);
                //                    //                    $("#upCoverImgReco").attr("src", reArray[2]);
                //                    console.log(newV + "--" + reArray[2]);
                //                }


            }
        });


    </script>
}